#include "GenUtils.h"

////////////// address <-> coordinates \\\\\\\\\\\\\\\

/**
*  Takes an address and returns x and y coordinates.
*/
int *addrToCoords(int addr){
    int *ret= new int[2]; //return {x,y}
    ret[0] = addr%XDIM;
    ret[1] = addr/XDIM;
    return ret;
}

/**
*  Takes x and y coordinates and returns an address.
*/
int coordsToAddr(int x, int y){
    int retAddr = y*XDIM + x;
    return retAddr;
}

//////////////// Point distance \\\\\\\\\\\\\\\\\

/**
*  Simple pythagorian distance between two points.
*/
double pythagoras(int valx, int valy){
    double answer = sqrt(valx*valx + valy*valy);
    return answer;
}

/**
*  Finds the distance between two points given as addresses.
*/
double pointDist(int point1, int point2){
    int *pt1 = addrToCoords(point1);
    int *pt2 = addrToCoords(point2);
    int dx = pt1[0]-pt2[0];
    int dy = pt1[1]-pt2[1];
    double length = pythagoras(dx,dy);
    return length;
}

#if 0
//Not currently using these
//////////////// Sorting \\\\\\\\\\\\\\\\\


/**
*  Takes an array of integers and returns a sorted array 
*  in which values are the indices from the input array.
*
*  NOTE: Though this is not pretty, it works fine for our purposes for now.
*/
int* sortInts(int* toSort, int len, bool big_to_small){
    int* vals = new int[len];              //make a copy
    for(int k=0; k<len; k++){
        vals[k] = toSort[k];
    }
    int* inds = new int[len];
    for(int i=0; i<len; i++){
	int min = 1000;
	int minInd = -1;
	for(int j=0; j<len; j++){ 
 	    if(vals[j]< min) {
	        min = vals[j];
	        minInd = j;
	    }		
	}
	inds[i] = minInd;
	vals[minInd] = 1000;		//scorched earth policy
    }
    if(big_to_small){
        int *temp = new int[len];
        for(int i=0; i<len; i++)
            temp[i] = inds[len-i-1];
        inds = temp;
    }
    return inds;
}

/**
*  Same as above, but with a double array.
*/
int* sortDoubs(double* toSort, int len, bool big_to_small){
    double* vals = new double[len];              //make a copy
    for(int k=0; k<len; k++){
        vals[k] = toSort[k];
    }
    int* inds = new int[len];
    for(int i=0; i<len; i++){
	double min = 1000;
	int minInd = -1;
	for(int j=0; j<len; j++){ 
 	    if(vals[j]< min) {
	        min = vals[j];
	        minInd = j;
	    }		
	}
	inds[i] = minInd;
	vals[minInd] = 1000;		//scorched earth policy
    }
    if(big_to_small){
        int *temp = new int[len];
        for(int i=0; i<len; i++)
            temp[i] = inds[len-i-1];
        inds = temp;
    }
    return inds;
}
#endif
